package com.zh.note.huawei.onehundred.光伏场地建设规划;

/**
 * @Author: zhanghuan
 * @date: 2023/8/7 10:28
 * @description: create it
 */
public class Main {
    public static void main(String[] args) {
        // Scanner sc = new Scanner(System.in);
        // int row = sc.nextInt(); // 行
        // int col = sc.nextInt();// 列
        // int width = sc.nextInt();
        // int pow = sc.nextInt();
        // int[][] region = new int[row][col];
        // for (int i = 0; i < row; i++) {
        //     for (int j = 0; j < col; j++) {
        //         region[i][j] = sc.nextInt();
        //     }
        // }

        int row = 2;
        int col = 5;
        int width = 2;
        int pow = 6;
        int[][] region = {{1, 3, 4, 5, 8}, {2, 3, 6, 7, 1}};

        int[][] dp = calculatePrefixSum(region, row, col);
        int res = 0;
        for (int i = 0; i < row - width + 1; i++) {
            for (int j = 0; j < col - width + 1; j++) {
                int count = subMatrixSum(dp, i, j, width);
                if (count >= pow) {
                    res++;
                }
            }
        }
        System.out.println(res);
    }

    private static int subMatrixSum(int[][] dp, int row, int col, int width) {
        int rowstart = row;
        int rowEnd = row + width - 1;
        int colStart = col;
        int colEnd = col + width - 1;
        int temp = dp[rowEnd + 1][colEnd + 1] - dp[rowEnd + 1][colStart] - dp[rowstart][colEnd + 1] + dp[rowstart][colStart];
        return temp;
    }

    private static int[][] calculatePrefixSum(int[][] matrix, int row, int col) {
        int[][] dp = new int[row + 1][col + 1];
        for (int i = 1; i <= row; i++) {
            for (int j = 1; j <= col; j++) {
                dp[i][j] = dp[i - 1][j] + dp[i][j - 1] - dp[i - 1][j - 1] + matrix[i - 1][j - 1];
            }
        }
        return dp;
    }
}
